import {
  querySysDepart,
  querySysDepartPerson,
  postSysDepartPerson,
  postSysDepart
} from '../services/system';

export default {
  namespace: 'sysdepart',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    personData:{
      list: [],
      pagination: {},
    },
  },

  effects: {
    *fetchTree({ payload }, { call, put }) {
      const response = yield call(querySysDepart, payload);
      yield put({
        type: 'save',
        payload: {
          data:response,
        }
      });
    },
    *fetchPerson({ payload }, { call, put }) {
      const response = yield call(querySysDepartPerson, payload);
      yield put({
        type: 'save',
        payload:{
          personData:response,
        }
      });
    },
    *postTree({ payload }, { call, put }) {
      const response = yield call(postSysDepart, payload);
      yield put({
        type: 'save',
        payload: {
          data:response,
        },
      });
    },
    // 新增删除修改
    *postPerson({ payload }, { call, put }) {
      const response = yield call(postSysDepartPerson, payload);

      if(response.success){
        yield put({
          type: 'fetchPerson',
          payload:{
            id:response.id
          }
        });
      }
    }
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
    clear() {
      return {
        data: {
          list: [],
          pagination: {},
        },
        personData:{
          list: [],
          pagination: {},
        }
      };
    },
  },
};
